package com.ljy.my_study.io.BIO.client;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/** 
* @author James
* @date 2018年11月5日 
*/
public class Client {
	Socket socket;
	public Client(Socket socket) {
		this.socket=socket;
	}
	
	public void send() throws IOException {
		String msg=null;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw=new PrintWriter(new BufferedOutputStream(socket.getOutputStream()),true);
		System.out.println("发送端："+Thread.currentThread().getName());
		while(true) {
			if((msg=br.readLine())!=null) {
				pw.println("发送者（"+Thread.currentThread().getName()+"）："+msg);
			}
		}
		
	}
	
	public void ascept() throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
		String line=null;
		while(true) {
			line=br.readLine();
			if(line!=null) {
				System.out.println(line);
			}
		}
	}
	
	public static void main(String[] args) throws UnknownHostException, IOException {
		Thread.currentThread().setName("thread_"+System.currentTimeMillis());
		Client c=new Client(new Socket("127.0.0.1",2333));
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					c.ascept();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
		try {
			c.send();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}
